/*
 * @Author: 0x9DEFA478
 * @Date: 2023-02-21 20:08:20
 * @LastEditTime: 2023-03-09 21:32:59
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "ListviewPinAssignment.h"




static bool StringLessString(const QString& s0,const QString& s1){

  const qsizetype l0=s0.length();
  const qsizetype l1=s1.length();

  qsizetype minlen=(l0<l1)?l0:l1;
  for(qsizetype i=0;i<minlen;i++){
    char16_t c0=s0[i].unicode();
    char16_t c1=s1[i].unicode();

    if(c0<c1){
      return true;
    }else if(c0>c1){
      return false;
    }
  }

  if(l0<l1){
    return true;
  }

  return false;
}

static bool StringLessString(const QString&& s0,const QString&& s1){
  return StringLessString(s0,s1);
}

static qsizetype FindFirstEq(QList<ListviewPinAssignment::Function*>& EqFunctions,ListviewPinAssignment::Function* Functions,qsizetype NumOfFunctions,const QString& s){
  qsizetype r;

  qsizetype r_m=s.length();
  for(r=0;r<r_m;r++){

    const QChar cs=s[0];
    for(qsizetype i=0;i<NumOfFunctions;i++){
      QString name=Functions[i].Name();

      if(r>=name.length()){
        continue;
      }

      const QChar c0=name[r];
      if(c0.unicode()==cs.unicode()){

        EqFunctions<<&Functions[i];

        for(qsizetype j=i+1;j<NumOfFunctions;j++){
          QString name=Functions[j].Name();
          if(r>=name.length()){
            continue;
          }
          if(name[r].unicode()==c0.unicode()){
            EqFunctions<<&Functions[j];
          }
        }

        return r;
      }
    }
  }

  return r;
}

static qsizetype StringSameLength(const QString& s0,qsizetype Index0,const QString& s1,qsizetype Index1=0){
  qsizetype r;
  qsizetype l0=s0.length();
  qsizetype l1=s1.length();

  r=0;
  while((Index0<l0)&&(Index1<l1)){
    char16_t c0=s0[Index0].unicode();
    char16_t c1=s1[Index1].unicode();
    
    if(c0!=c1){
      break;
    }

    r++;
    Index0++;
    Index1++;
  }

  return r;
}

static bool StringSameString(const QString& s0,const QString& s1,const QString& s,qsizetype Index){
  
  qsizetype sl0=StringSameLength(s0,Index,s);
  qsizetype sl1=StringSameLength(s1,Index,s);

  if(sl0>sl1){
    return true;
  }
  
  if(sl0==sl1){
    return StringLessString(s0,s1);
  }

  return false;
}

static bool StringSameString(const QString&& s0,const QString&& s1,const QString&& s,qsizetype Index){
  return StringSameString(s0,s1,s,Index);
}




ListviewPinAssignment::ListviewPinAssignment(QWidget *parent):QListView(parent){

  // setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);

  // connect(this,&ListviewPinAssignment::customContextMenuRequested,this,&ListviewPinAssignment::SlotContextMenu);
  connect(this,&ListviewPinAssignment::clicked,this,&ListviewPinAssignment::SlotClicked);

  
  Model=new QStandardItemModel(this);

  Functions=nullptr;
  SearchText="";
  ShowFunction.clear();

  
  setModel(Model);

}

ListviewPinAssignment::~ListviewPinAssignment(){

  if(this->Functions!=nullptr){
    delete[] this->Functions;
  }

}




void ListviewPinAssignment::SlotClicked(const QModelIndex& index){
  
  if(index.isValid()==false){
    return;
  }

  emit Clicked(ShowFunction[index.row()]->V());
  
}




void ListviewPinAssignment::SetData(const QList<Function*>& data){
  qsizetype count;

  if(this->Functions!=nullptr){
    delete[] this->Functions;
  }

  count=data.count();
  NumberFunction=count;

  this->Functions=new Function[count];

  QList<Function*> Rank;

  Rank<<data[0];

  for(qsizetype i=1;i<count;i++){
    Function* f=data[i];

    qsizetype dCount=Rank.count();
    qsizetype dIndex;
    for(dIndex=0;dIndex<dCount;dIndex++){
      if(StringLessString(f->Name(),Rank[dIndex]->Name())){
        Rank.insert(dIndex,f);
        break;
      }
    }
    if(dIndex==dCount){
      Rank.append(f);
    }
  }

  for(int i=0;i<count;i++){
    this->Functions[i].SetParam(Rank[i][0]);
  }

  setSearchText(SearchText);
}

void ListviewPinAssignment::setSearchText(const QString& s){

  qsizetype s_length;

  ShowFunction.clear();

  s_length=s.length();
  if(s_length==0){
    
    Model->setRowCount(NumberFunction);
    for(int i=0;i<NumberFunction;i++){
      QStandardItem* item=new QStandardItem();

      item->setText(Functions[i].Name());
      item->setBackground(Functions[i].BgColor());
      
      item->setEditable(false);
      Model->setItem(i,item);
      ShowFunction<<&Functions[i];
    }

    return;
  }

  //寻找第一个字符匹配的项，并且得到索引
  qsizetype Index;
  QList<Function*> functions;

  Index=FindFirstEq(functions,Functions,NumberFunction,s);
  if(0==functions.count()){
    Model->clear();
    return;
  }

  //进行相似性排序
  QList<Function*>& Rank=ShowFunction;
  {
    qsizetype count=functions.count();

    Rank<<functions[0];

    for(qsizetype i=1;i<count;i++){
      Function* f=functions[i];

      qsizetype dCount=Rank.count();
      qsizetype dIndex;
      for(dIndex=0;dIndex<dCount;dIndex++){
        if(StringSameString(f->Name(),Rank[dIndex]->Name(),s,Index)){
          Rank.insert(dIndex,f);
          break;
        }
      }
      if(dIndex==dCount){
        Rank.append(f);
      }
    }
  }

  {
    qsizetype count=Rank.count();

    Model->setRowCount(count);

    for(qsizetype i=0;i<count;i++){
      QStandardItem* item=new QStandardItem();

      item->setText(Rank[i]->Name());
      item->setBackground(Rank[i]->BgColor());

      item->setEditable(false);
      Model->setItem(i,item);
    }
  }

}



